package com.example.soap.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection;

/**
 * SOAP端点配置 访问路径 {host}:port/ws/soap.wsdl
 *
 * @author LiuGangQiang Create in 2025/09/04
 */
@EnableWs
@Configuration
public class SoapConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }

    @Bean(name = "soap")
    public DefaultWsdl11Definition definition(CommonsXsdSchemaCollection collection) {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setPortTypeName("SoapPort");
        definition.setLocationUri("/ws");
        definition.setTargetNamespace("http://www.example.com/soap");
        definition.setSchemaCollection(collection);
        definition.setRequestSuffix("Request");
        definition.setResponseSuffix("Response");
        return definition;
    }

    @Bean
    public CommonsXsdSchemaCollection collection() {
        CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection();
        Resource[] resources = new Resource[] { new ClassPathResource("soap.xsd"), new ClassPathResource("cxmt.xsd") };
        collection.setXsds(resources);
        collection.setInline(true);
        return collection;
    }
}
